/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProjectInfo.java
 *
 * Created on Jul 2, 2011, 7:47:07 PM
 */

package javadatabasegui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;

/**
 *
 * @author PM
 */
public class ProjectInfo extends javax.swing.JFrame {
    int projectID;
    String userEmail;
    Connection con;
    Boolean newPrj = true;
    private String PDesc = "Project Description";
    private String PName= "Project Name";
    private String dashName = "NULL";

    /** Creates new form ProjectInfo */
    public ProjectInfo(int prjID, String uemail, Connection conn) {
        projectID = prjID;
        userEmail = uemail;
        con = conn;
        newPrj = false;
        setProjectName();
        setUserName();
        initComponents();
        getTeam();
    }

    public ProjectInfo( String uemail, Connection conn) {
        userEmail = uemail;
        con = conn;
        setUserName();
        initComponents();
        getTeam();
    }

    private void setUserName(){
        try{
            Statement stmt = con.createStatement();
            ResultSet rs = null;
            if (stmt.execute("SELECT F_Name,L_Name FROM "
                    + "cs4400_Group_14.Users WHERE Users.Email =\""
                    + userEmail + "\"")){
                rs = stmt.getResultSet();
            }
            rs.next();
            dashName = rs.getString("F_Name") + " " + rs.getString("L_Name");
        }catch(SQLException e){
            System.err.println(e.getMessage());
            System.err.println(e.getSQLState());
        }
    }
    
    //get the name and description of the project.
    private void setProjectName(){
        try {
            Statement stmt = con.createStatement();
            ResultSet rs = null;
            if (stmt.execute("SELECT P_Name, P_Desc FROM cs4400_Group_14.Project WHERE PID=\""+projectID+"\""))
                rs = stmt.getResultSet();
            rs.next();
            PName = rs.getString("P_Name");
            PDesc = rs.getString("P_Desc");
        }catch(SQLException e){ System.err.println(e.getMessage()); }
    }   
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        dashboardLabel = new javax.swing.JLabel();
        pmilestoneLabel = new javax.swing.JLabel();
        PIDlabel = new javax.swing.JLabel();
        PrTitleLabel = new javax.swing.JLabel();
        DescLabel = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        ProjManLabel = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel11 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("GT Project Manager - Project Info");
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setText("Project Info");

        dashboardLabel.setFont(new java.awt.Font("Tahoma", 0, 14));
        dashboardLabel.setForeground(java.awt.Color.blue);
        dashboardLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        dashboardLabel.setText("<html> <u>Dashboard</u></html>");
        dashboardLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        dashboardLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                dashboardLabelMouseClicked(evt);
            }
        });

        pmilestoneLabel.setFont(new java.awt.Font("Tahoma", 0, 14));
        pmilestoneLabel.setForeground(java.awt.Color.blue);
        pmilestoneLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        pmilestoneLabel.setText("<html> <u> Project Milestones </u> </html>");
        pmilestoneLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        pmilestoneLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                pmilestoneLabelMouseClicked(evt);
            }
        });

        PIDlabel.setFont(new java.awt.Font("Tahoma", 0, 16));
        PIDlabel.setText("Project ID");

        PrTitleLabel.setFont(new java.awt.Font("Tahoma", 0, 16));
        PrTitleLabel.setText("Project Title");

        DescLabel.setFont(new java.awt.Font("Tahoma", 0, 16));
        DescLabel.setText("Description");

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel7.setText("(assigned automatically)");

        jTextField1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jTextField1.setText(PName);

        jTextArea1.setColumns(20);
        jTextArea1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jTextArea1.setRows(5);
        jTextArea1.setText(PDesc);
        jScrollPane1.setViewportView(jTextArea1);

        ProjManLabel.setFont(new java.awt.Font("Tahoma", 0, 16));
        ProjManLabel.setText("Project Manager");

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel9.setText(dashName);

        jPanel2.setBackground(new java.awt.Color(226, 232, 255));

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 16));
        jLabel10.setText("Project Team");
        jLabel10.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        jLabel11.setText("Check the box next to the name of each team member.");

        jScrollPane2.setBackground(new java.awt.Color(226, 232, 255));
        jScrollPane2.setBorder(null);
        jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        jPanel4.setBackground(new java.awt.Color(226, 232, 255));

        jPanel5.setBackground(new java.awt.Color(226, 232, 255));

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 89, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 267, Short.MAX_VALUE)
        );

        jPanel6.setBackground(new java.awt.Color(226, 232, 255));

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 525, Short.MAX_VALUE)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 267, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jScrollPane2.setViewportView(jPanel4);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 660, Short.MAX_VALUE)
                            .addComponent(jLabel10)
                            .addComponent(jLabel11, javax.swing.GroupLayout.DEFAULT_SIZE, 660, Short.MAX_VALUE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 620, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 5, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel3.setBackground(new java.awt.Color(230, 174, 39));

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jButton1.setText("Save");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(561, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(PrTitleLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                                .addComponent(PIDlabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                                .addComponent(DescLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(ProjManLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(41, 41, 41)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jScrollPane1)
                            .addComponent(jTextField1)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 447, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 382, Short.MAX_VALUE)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(dashboardLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(pmilestoneLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(dashboardLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pmilestoneLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PIDlabel, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(PrTitleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(DescLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ProjManLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void dashboardLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_dashboardLabelMouseClicked
        new UserDashboard(userEmail, con).setVisible(true);
            this.dispose();
    }//GEN-LAST:event_dashboardLabelMouseClicked

    private void pmilestoneLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pmilestoneLabelMouseClicked
        if(newPrj == false){
            new ProjectMilestones(projectID, userEmail, con).setVisible(true);
            this.dispose();
        }else{
            JOptionPane.showMessageDialog(null, "Warning: Must save new project before viewing milestones.");
        }
    }//GEN-LAST:event_pmilestoneLabelMouseClicked

    //SAVE BUTTON IS CLICKED
    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        String pTitle = jTextField1.getText();
        String pDesc = jTextArea1.getText();
        boolean saveOK = true;
        if(pTitle.equals("") || pDesc.equals("")){
            JOptionPane.showMessageDialog(null, "Fields cannot be empty.");
            return;
        }
        
        Component[] cmpList = jPanel5.getComponents();
        javax.swing.JCheckBox[] checkBoxes = new javax.swing.JCheckBox[cmpList.length];
        ArrayList teamMembers = new ArrayList();
        ArrayList teamMembersRemoved = new ArrayList();
        for(int i=0; i < checkBoxes.length; i++){
            checkBoxes[i] = (JCheckBox) cmpList[i];
            if(checkBoxes[i].isSelected())
                teamMembers.add(checkBoxes[i].getName());
            else
                teamMembersRemoved.add(checkBoxes[i].getName());
        }

        if(newPrj == false){
            try{
                Statement stmt = con.createStatement();
                ResultSet rs = null;
                if (stmt.execute("SELECT DISTINCT U_Email FROM `cs4400_Group_14`.`Tasks` WHERE PID = "+ projectID +" AND `Status` = 1")){
                    rs = stmt.getResultSet();
                }
                for(int i = 0; rs.next(); i++){
                    for( int y = 0; y<teamMembersRemoved.size(); y++){
                        if(teamMembersRemoved.get(y).equals(rs.getString("U_Email"))){
                            saveOK = false;
                        }
                    }
                }
            }catch(SQLException e){
                System.err.println("Error getting distinct U_Email");
                System.err.println(e.getMessage());
            }
        }
        
        if(saveOK == false){
            JOptionPane.showMessageDialog(null, "Cannot remove user with active tasks.");
        }else{
            if(newPrj == true){
                try{
                    //Add the new project to the database
                    Statement stmt = con.createStatement();

                    stmt.execute("INSERT INTO `cs4400_Group_14`.`Project` (`P_Name`, `P_Desc`, `Mgr_Email`) VALUES ('"
                            + pTitle +"', '"+ pDesc +"', '"+ userEmail +"')");

                    //Get the project ID for the project created.
                    Statement stmt1 = con.createStatement();
                    ResultSet rs1 = null;
                    if (stmt1.execute("SELECT MAX(PID) FROM `cs4400_Group_14`.`Project`")){
                        rs1 = stmt1.getResultSet();
                    }
                    rs1.next();
                    projectID = rs1.getInt("MAX(PID)");

                    //Insert the workers into the User_WP table for this project.
                    Statement stmt2 = con.createStatement();
                    for(int i = 0; i < teamMembers.size(); i++){
                        stmt2.execute("INSERT INTO `cs4400_Group_14`.`User_WP` (`Email`, `PID`) Values ('"
                                + teamMembers.get(i) + "', '" + projectID + "')");
                    }
                    
                    //Insert the one creating the project into the User_WP table
                    Statement stmt3 = con.createStatement();
                    stmt3.execute("INSERT INTO `cs4400_Group_14`.`User_WP` (`Email`, `PID`) Values ('"
                                + userEmail + "', '" + projectID + "')");
                    
                }catch(SQLException e){
                    System.err.println("Error trying to insert and get max PID");
                    System.err.println(e.getMessage());
                }
            }else{
                try{
                    //Update the new project to the database
                    Statement stmt = con.createStatement();
                    stmt.execute("Update `cs4400_Group_14`.`Project` SET `P_Name` = '"
                            + pTitle + "', `P_Desc` = '" + pDesc + "' WHERE `PID` = '" + projectID +"'");
                }catch(SQLException e){
                    System.err.println("Error trying to update the project");
                    System.err.println(e.getMessage());
                }
                
                //Check to see if selected workers for current project are still in project, if not then add.
                try{
                    Statement stmt = con.createStatement();                    
                    Statement stmt2 = con.createStatement();
                    Statement stmt3 = con.createStatement();
                    ResultSet rs = null;

                    if (stmt.execute("SELECT Email FROM `cs4400_Group_14`.`User_WP` WHERE PID = "+ projectID )){
                        rs = stmt.getResultSet();
                    }

                    //Adds users to User_WP if they are not already in project
                    Boolean tnt = false;
                    for(int i = 0; i<teamMembers.size(); i++){
                        rs.first();
                        for(int y = 0; rs.next(); y++){
                            if(y==0){ rs.previous(); }
                            if(teamMembers.get(i).equals(rs.getString("Email"))){
                                tnt = true;
                            }
                        }
                        if(tnt == false){
                            stmt2.execute("INSERT INTO `cs4400_Group_14`.`User_WP` (`Email`, `PID`) Values ('"
                                + teamMembers.get(i) + "', '" + projectID + "')");
                        }
                        tnt = false;
                    }

                    //This will remove the users from the User_WP project.
                    tnt = false;
                    for(int i = 0; i<teamMembersRemoved.size(); i++){
                        rs.first();
                        for(int y = 0; rs.next(); y++){
                            if(y==0){ rs.previous(); }
                            if(teamMembersRemoved.get(i).equals(rs.getString("Email"))){
                                tnt = true;
                            }
                        }
                        if(tnt == true){
                            stmt3.execute("DELETE FROM `cs4400_Group_14`.`User_WP` WHERE `Email`='"+teamMembersRemoved.get(i)+"' and`PID`='"+projectID+"'");
                        }
                        tnt = false;
                    }
                }catch(SQLException e){
                    System.err.println("Error in sql statements updating an existing project");
                    System.err.println(e.getMessage());
                } 
            }
        }
        new UserDashboard(userEmail, con).setVisible(true);
                this.dispose();
    }//GEN-LAST:event_jButton1MouseClicked

    private void getTeam(){
        int numEntries = 0;

        //Gets the number of entries in the box, needed to set the size of the box (for scrolling).
        try{
            Statement stmt = con.createStatement();
            ResultSet rs = null;
            if (stmt.execute("SELECT COUNT(Email) from `cs4400_Group_14`.`Users` WHERE Admin = 0")){
                rs = stmt.getResultSet();
            }
            rs.next();
            numEntries = rs.getInt("COUNT(Email)")-1;
        }catch(SQLException e){
            System.err.println("Error in Project Count");
            System.err.println(e.getMessage());
            }

        //Sets the size of the panel to the correct height for the number of entries.
        jPanel4.setPreferredSize(new java.awt.Dimension(620, numEntries*25));
        jPanel5.setPreferredSize(new java.awt.Dimension(64, numEntries*25));
        jPanel6.setPreferredSize(new java.awt.Dimension(550, numEntries*25));

        //Creats an array type to hold the labels (labels are each line in the panel)
        javax.swing.JLabel[] tstLabel = new javax.swing.JLabel[numEntries];

        //Fills the array with new labels.
        for(int i=0; i<tstLabel.length; i++){
            tstLabel[i] = new javax.swing.JLabel();
        }

        //Create an array for checkboxes and fill it with checkboxes
        javax.swing.JCheckBox[] checkTeam = new javax.swing.JCheckBox[numEntries];
        for(int i=0; i<checkTeam.length; i++){
            checkTeam[i] = new javax.swing.JCheckBox();
        }

        //This try catch block will get the data from the database for each line
        //that we want to show. It will put that data into the labels of the array
        //created above.
        try{
            Statement stmt = con.createStatement();
            Statement stmt1 = con.createStatement();
            ResultSet rs = null;
            ResultSet rs1 = null;
            if (stmt.execute("SELECT F_Name, L_Name, Email FROM `cs4400_Group_14`.`Users`"
                    + " WHERE Admin = 0 AND Email !=\"" + userEmail +"\" ORDER BY L_Name")){
                rs = stmt.getResultSet();
            }
            
            //This gets the list of emails that are working on the project, for the checkboxes.
            if (stmt1.execute("SELECT Email From `cs4400_Group_14`.`User_WP`"
                    + " WHERE PID =\"" + projectID +"\" AND Email != \""+ userEmail+"\"")){
                rs1 = stmt1.getResultSet();
            }

            //This creates an array list to hold the emails. Array lists are resizeable.
            ArrayList teamEmail = new ArrayList();
            for(int i=0; rs1.next();i++){
                teamEmail.add(rs1.getString("Email"));
            }

            for(int i=0; rs.next();i++){
                tstLabel[i].setText("<html> <body> <table width=\"500\"> <tr>  <td width=\"50%\"> <span style=\"color:blue\"> <u>" + rs.getString("F_Name")
                        + "  <span style=\"color:blue\">" + " " + rs.getString("L_Name")
                        + "</u> <td width=\"50%\">" + rs.getString("Email") + "</td> </tr> </table> </body> </html>");
                tstLabel[i].setName(rs.getString("Email"));
                checkTeam[i].setName(rs.getString("Email"));

                //checking to see if the current tuple email is part of project.
                if (teamEmail.contains(rs.getString("Email"))){
                    checkTeam[i].setSelected(true);
                }
                else{
                    checkTeam[i].setSelected(false);
                }
            }
        }catch(SQLException e){
            System.err.println("Error in displaying team");
            System.err.println(e.getMessage());
            }

        //this is much easier to display the components in the panel
        for(int i=0; i<tstLabel.length; i++){
            tstLabel[i].setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
            tstLabel[i].addMouseListener(new java.awt.event.MouseAdapter() {
                @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                employeeClicked(evt);
            }});
        }

        jPanel5.setLayout(new GridBagLayout());
        jPanel6.setLayout(new GridBagLayout());
        GridBagConstraints cnst = new GridBagConstraints();
        GridBagConstraints cnst1 = new GridBagConstraints();
        for(int i=0; i<tstLabel.length; i++){
            checkTeam[i].setBackground(new Color(226,232,255));
            tstLabel[i].setFont(new Font(tstLabel[i].getText(),4,12));
            cnst1.gridx = 0;
            cnst1.gridy = i;
            cnst1.ipadx = 0;
            cnst1.ipady = 1;
            jPanel5.add(checkTeam[i],cnst1);
            cnst.gridx = 0;
            cnst.gridy = i;
            jPanel6.add(tstLabel[i],cnst);
        }
        
    }


    private void employeeClicked(java.awt.event.MouseEvent evt ){
        Component cmp = evt.getComponent();
        new ViewEmp(cmp.getName(), con).setVisible(true);
           //this.dispose();
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel DescLabel;
    private javax.swing.JLabel PIDlabel;
    private javax.swing.JLabel PrTitleLabel;
    private javax.swing.JLabel ProjManLabel;
    private javax.swing.JLabel dashboardLabel;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JLabel pmilestoneLabel;
    // End of variables declaration//GEN-END:variables

}
